<?php

namespace fast;

class AliyunUtil
{
    /**
     * 图片域名
     * @var string
     */
    const IMG_HOST = 'http://dx-sy-photo.oss-cn-shanghai.aliyuncs.com/';
    const IMG_HOST_V2 = 'http://dx-sy-img.oss-cn-shanghai.aliyuncs.com/';

    /**
     * 音频域名
     * @var string
     */
    const FILE_HOST = 'http://dx-sy-voice.oss-cn-shanghai.aliyuncs.com/';

    /**
     * 阿里云外网域名
     * @var string
     */
    const END_POINT = "http://oss-cn-shanghai.aliyuncs.com";

    /**
     * 图片缩略裁剪构造
     * @param $url
     * @param int $width
     * @param int $height
     */
    public static function imageResize($url, $width = 500, $height = 500)
    {
        if (!$url) return '';
        return $url . "?x-oss-process=image/resize,m_fill,h_{$height},w_{$width}";
    }

    /**
     * 返回资源的绝对地址
     *
     * @param string $resource
     * @param string $type   img|voice
     * @return string
     */
    public static function buidResUrl($resource, $type)
    {
        $res = trim($resource);
        if ($res == '') return '';

        switch ($type) {
            case 'img':
                $res = self::IMG_HOST . $res;
                break;
            case 'voice':
                $res = self::FILE_HOST . $res;
                break;
        }

        return $res;
    }

    /**
     * 模糊图片
     */
    static function blurImage($url = '')
    {
        if (!$url) return '';
        return self::IMG_HOST . $url . '?x-oss-process=image/blur,r_11,s_11/bright,-50';
    }
}
